package com.itheima.sfbx.insurance.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.sfbx.framework.commons.constant.basic.SuperConstant;
import com.itheima.sfbx.framework.commons.exception.ProjectException;
import com.itheima.sfbx.framework.commons.utils.BeanConv;
import com.itheima.sfbx.insurance.dto.CoefficentVO;
import com.itheima.sfbx.insurance.enums.CoefficentEnum;
import com.itheima.sfbx.insurance.mapper.CoefficentMapper;
import com.itheima.sfbx.insurance.pojo.CategoryCoefficent;
import com.itheima.sfbx.insurance.pojo.Coefficent;
import com.itheima.sfbx.insurance.pojo.InsuranceCoefficent;
import com.itheima.sfbx.insurance.service.ICategoryCoefficentService;
import com.itheima.sfbx.insurance.service.ICoefficentService;
import com.itheima.sfbx.insurance.service.IInsuranceCoefficentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Description：系数项服务实现类
 */
@Slf4j
@Service
public class CoefficentServiceImpl extends ServiceImpl<CoefficentMapper, Coefficent> implements ICoefficentService {
     @Autowired
     private ICategoryCoefficentService categoryCoefficentService;
     @Autowired
     private IInsuranceCoefficentService insuranceCoefficentService;

    private LambdaQueryWrapper<Coefficent> queryWrapper(CoefficentVO coefficentVO){
         LambdaQueryWrapper<Coefficent> wrapper = new LambdaQueryWrapper<>();
         wrapper
                 .eq(StrUtil.isNotBlank(coefficentVO.getCoefficentKey()),Coefficent::getCoefficentKey,coefficentVO.getCoefficentKey())
                 .eq(StrUtil.isNotBlank(coefficentVO.getDataState()),Coefficent::getDataState,coefficentVO.getDataState())
                 .like(StrUtil.isNotBlank(coefficentVO.getCoefficentKeyName()),Coefficent::getCoefficentKeyName,coefficentVO.getCoefficentKeyName());
         return wrapper;
     }


    @Override
    public Page<CoefficentVO> findPage(CoefficentVO coefficentVO, int pageNum, int pageSize) {
        LambdaQueryWrapper<Coefficent> wrapper = queryWrapper(coefficentVO);
        Page<Coefficent> page = this.page(new Page<>(pageNum, pageSize),wrapper);
        Page<CoefficentVO> voPage = BeanConv.toPage(page, CoefficentVO.class);
        return voPage;
    }

    @Override
    public CoefficentVO findById(String coefficentId) {
        Coefficent coefficent = this.getById(coefficentId);
        if(coefficent==null){
            return null;
        }
        return BeanConv.toBean(coefficent, CoefficentVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CoefficentVO save(CoefficentVO coefficentVO) {
        //查询系数项编号是否存在
        Integer count = this.lambdaQuery()
                .eq(Coefficent::getCoefficentKey, coefficentVO.getCoefficentKey())
                .eq(Coefficent::getDataState, SuperConstant.DATA_STATE_0)
                .count();
        if(count>0){
            throw new ProjectException(CoefficentEnum.SAVE_FAIL);
        }
        //保存系数项
        Coefficent bean = BeanConv.toBean(coefficentVO, Coefficent.class);
        this.save(bean);
        return BeanConv.toBean(bean,CoefficentVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(CoefficentVO coefficentVO) {
        //对象是否存在
        Coefficent coefficent = this.getById(coefficentVO.getId());
        if(coefficent==null){
            throw new ProjectException(CoefficentEnum.UPDATE_FAIL);
        }
        //判断key是否之前设置过
        Integer count = this.lambdaQuery()
                .eq(Coefficent::getCoefficentKey, coefficentVO.getCoefficentKey())
                .ne(Coefficent::getId, coefficentVO.getId())
                .count();
        if(count>0){
            throw new ProjectException(CoefficentEnum.UPDATE_FAIL);
        }
        //判断之前的key是否绑定了关系
        Integer count1 = categoryCoefficentService.lambdaQuery()
                .eq(CategoryCoefficent::getCoefficentKey, coefficent.getCoefficentKey())
                .eq(CategoryCoefficent::getDataState, SuperConstant.DATA_STATE_0)
                .count();
        Integer count2 = insuranceCoefficentService.lambdaQuery()
                .eq(InsuranceCoefficent::getCoefficentKey, coefficent.getCoefficentKey())
                .eq(InsuranceCoefficent::getDataState, SuperConstant.DATA_STATE_0)
                .count();
        if(count1>0||count2>0){
            throw new ProjectException(CoefficentEnum.UPDATE_FAIL);
        }
        return this.updateById(BeanConv.toBean(coefficentVO,Coefficent.class));
    }

    @Override
    @Transactional
    public Boolean delete(String[] checkedIds) {
        List<String> ids = Arrays.asList(checkedIds);
        List<Coefficent> list = this.lambdaQuery()
                .in(Coefficent::getId, ids)
                .list();
        if(CollUtil.isEmpty(list)){
            return true;
        }
        Set<String> collect = list.stream().map(Coefficent::getCoefficentKey).collect(Collectors.toSet());
        //判断之前的key是否绑定了关系
        Integer count1 = categoryCoefficentService.lambdaQuery()
                .in(CategoryCoefficent::getCoefficentKey, collect)
                .eq(CategoryCoefficent::getDataState, SuperConstant.DATA_STATE_0)
                .count();
        Integer count2 = insuranceCoefficentService.lambdaQuery()
                .in(InsuranceCoefficent::getCoefficentKey, collect)
                .eq(InsuranceCoefficent::getDataState, SuperConstant.DATA_STATE_0)
                .count();
        if(count1>0||count2>0){
            throw new ProjectException(CoefficentEnum.DEL_FAIL);
        }
        return this.removeByIds(ids);
    }

    @Override
    public List<CoefficentVO> findList(CoefficentVO coefficentVO) {
        LambdaQueryWrapper<Coefficent> wrapper = queryWrapper(coefficentVO);
        List<Coefficent> list = this.list(wrapper);
        if(CollUtil.isEmpty(list)){
            return null;
        }
        List<CoefficentVO> voList = BeanConv.toBeanList(list, CoefficentVO.class);
        return voList;
    }

    @Override
    public CoefficentVO findByCoefficentKey(String coefficentKey) {
        if(StrUtil.isBlank(coefficentKey)) {
            throw new ProjectException(CoefficentEnum.FIND_ONE_FAIL);
        }
        Coefficent coefficent = this.lambdaQuery()
                .eq(Coefficent::getCoefficentKey, coefficentKey)
                .one();
        if(coefficent==null){
            return null;
        }
        CoefficentVO vo = BeanConv.toBean(coefficent, CoefficentVO.class);
        return vo;
    }
}
